package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.AreaRecord;
import com.example.prison.service.AreaRecordService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("防区记录")
@RestController
@RequestMapping("/api")
public class AreaRecordController {

    @Autowired
    private AreaRecordService areaRecordService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取防区记录")
    @GetMapping("/getAreaRecord")
    public Response<PageInfo<AreaRecord>> getAreaRecord(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取防区记录");
        PageInfo<AreaRecord> pageInfo = areaRecordService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增防区记录")
    @PostMapping("/addAreaRecord")
    public Response<?> addAreaRecord(@RequestBody AreaRecord record) {
        log.info("新增防区记录");
//        systemLogService.add("新增防区记录");
        int result = areaRecordService.add(record);
        return Response.success("添加成功！");
    }

    @ApiOperation("依据防区段编号获取防区记录")
    @GetMapping("/getAreaRecordByDefenseSection")
    public Response<PageInfo<AreaRecord>> getAreaRecordByDefenseSection(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                    @Parameter(description = "防区段编号")@RequestParam String defenseSection) {
        log.info("依据设备名称获取防区记录");
        systemLogService.add("依据设备名称获取防区记录");
        PageInfo<AreaRecord> pageInfo = areaRecordService.getByDefenseSection(pageNumber, pageSize, defenseSection);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("无防区记录数据！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("依据警情状态获取防区记录")
    @GetMapping("/getAreaRecordByAlarmStatus")
    public Response<PageInfo<AreaRecord>> getAreaRecordByAlarmStatus(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                    @Parameter(description = "警情状态")@RequestParam String alarmStatus) {
        log.info("依据设备名称获取防区记录");
        systemLogService.add("依据设备名称获取防区记录");
        PageInfo<AreaRecord> pageInfo = areaRecordService.getByAlarmStatus(pageNumber, pageSize, alarmStatus);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("无防区记录数据！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("依据设备编号获取防区记录")
    @GetMapping("/getAreaRecordByDeviceNumber")
    public Response<PageInfo<AreaRecord>> getAreaRecordByDeviceNumber(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                    @Parameter(description = "设备编号")@RequestParam String deviceNumber) {
        log.info("依据设备名称获取防区记录");
        systemLogService.add("依据设备名称获取防区记录");
        PageInfo<AreaRecord> pageInfo = areaRecordService.getByDeviceNumber(pageNumber, pageSize, deviceNumber);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("无防区记录数据！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("依据时间获取防区记录")
    @GetMapping("/getAreaRecordByTime")
    public Response<PageInfo<AreaRecord>> getAreaRecordByTime(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                              @Parameter(description = "开始时间")@RequestParam String beginTime,
                                                              @Parameter(description = "结束时间")@RequestParam String endTime) {
        log.info("依据时间获取防区记录");
        systemLogService.add("依据时间获取防区记录");
        PageInfo<AreaRecord> pageInfo = areaRecordService.getByTime(pageNumber, pageSize, beginTime,endTime);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("无防区记录数据！");
        }
        return Response.success("获取成功！", pageInfo);
    }

}
